package itsm.isperp.module.service.project;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.project.ProjProject;
import itsm.isperp.module.entity.project.ProjProjectMember;
import itsm.isperp.module.entity.task.TskPlanTask;
import itsm.isperp.module.repository.task.TskPlanTaskMapper;
import itsm.isperp.module.service.slm.SlmServiceAgreementService;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2015-12-27
 * @since 1.0
 * 
 */
@Service
public class ProjProjectOnfileService {

	@Autowired
	ProjProjectService projProjectService;
	@Autowired
	ProjProjectMemberService projProjectMemberService;
	@Autowired
	ProjProjectServiceService projProjectServiceService;
	@Autowired
	ProjCostTemplateService projCostTemplateService;

	@Autowired
	TskPlanTaskMapper tskPlanTaskMapper;

	@Autowired
	SlmServiceAgreementService slmServiceAgreementService;

	@Transactional
	public void copyProject(final String oldProjectId) {

		ProjProject p = this.projProjectService.findOne(oldProjectId);

		// 1.基本信息
		// 2.人员
		// 3.服务
		// 4.计划任务
		// 5.服务级别
		Date ed = p.getEndDate();

		Calendar c = Calendar.getInstance();

		c.setTime(ed);
		int year = c.get(Calendar.YEAR) + 1;

		String name = p.getName();

		name = name.replaceAll(String.valueOf(year), String.valueOf(year + 1));

		p.setId(null);
		p.setCode(String.valueOf(year));

		p.setName(name);

		Calendar sed = Calendar.getInstance();
		Calendar ced = Calendar.getInstance();
		sed.setTime(p.getStartDate());
		ced.setTime(ed);

		sed.add(Calendar.YEAR, 1);
		ced.add(Calendar.YEAR, 1);

		p.setStartDate(sed.getTime());
		p.setEndDate(ced.getTime());

		projProjectService.saveOrUpdate(p);
		final String newProjectId = p.getId();

		List<ProjProjectMember> members = projProjectMemberService
				.findAllByProjectId(oldProjectId);

		for (ProjProjectMember m : members) {
			m.setId(null);
			m.setProjProjectId(newProjectId);
			projProjectMemberService.saveOrUpdate(m);
		}

		List<itsm.isperp.module.entity.project.ProjProjectService> services = projProjectServiceService
				.findAllByProjectId(oldProjectId);

		for (itsm.isperp.module.entity.project.ProjProjectService ser : services) {
			ser.setId(null);
			ser.setProjectId(newProjectId);
			projProjectServiceService.saveOrUpdate(ser);
		}

		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.addFilterRule("project_id", JqGridFilterRuleOp.eq, oldProjectId);

		List<TskPlanTask> tasks = tskPlanTaskMapper.findAllPlanTask(req);

		for (TskPlanTask t : tasks) {
			t.setId(HibernateUUIDGenerator.generate());
			t.setProjectId(newProjectId);
			tskPlanTaskMapper.save(t);
		}

//		slmServiceAgreementService.copyToProject(oldProjectId, newProjectId);

		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		jdbcTemplate.update("UPDATE icd_incident SET project_id='"
				+ newProjectId + "' WHERE project_id='" + oldProjectId
				+ "' AND solved_date IS NULL");

	}

}
